{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "314b9714",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pulp\n",
    "\n",
    "# 创建最大化问题\n",
    "prob = pulp.LpProblem(\"Production_Planning\", pulp.LpMaximize)\n",
    "\n",
    "# 定义数据\n",
    "Products = [\"Product1\", \"Product2\"]\n",
    "Profits = {\"Product1\": 25, \"Product2\": 30}\n",
    "\n",
    "Robots = [\"Robot1\", \"Robot2\"]\n",
    "maxtime = {\"Robot1\": 40, \"Robot2\": 45}\n",
    "costs = {\n",
    "    \"Product1\": {\"Robot1\": 2, \"Robot2\": 1},\n",
    "    \"Product2\": {\"Robot1\": 1, \"Robot2\": 3},\n",
    "}\n",
    "\n",
    "# 定义每个产品的总产量变量\n",
    "x = pulp.LpVariable.dicts(\"Prod\", Products, lowBound=0, cat=\"Continuous\")\n",
    "\n",
    "# 目标函数 - 基于产品总产量计算利润\n",
    "prob += pulp.lpSum([x[i] * Profits[i] for i in Products]), \"Total_Profit\"\n",
    "\n",
    "# 约束条件 - 每个机器人的总使用时间\n",
    "for j in Robots:\n",
    "    prob += pulp.lpSum([x[i] * costs[i][j] for i in Products]) <= maxtime[j], f\"Robot_{j}_Time\"\n",
    "\n",
    "# 求解问题\n",
    "prob.solve()\n",
    "\n",
    "# 输出结果\n",
    "print(f\"求解状态: {pulp.LpStatus[prob.status]}\")\n",
    "print(f\"最大利润: ${pulp.value(prob.objective):.2f}\\n\")\n",
    "\n",
    "print(\"最优生产方案:\")\n",
    "for i in Products:\n",
    "    print(f\"{i}: {x[i].value():.1f} 单位\")\n",
    "\n",
    "# 输出各机器人使用情况\n",
    "print(\"\\n各机器人使用时间:\")\n",
    "for j in Robots:\n",
    "    used_time = sum(x[i].value() * costs[i][j] for i in Products)\n",
    "    print(f\"{j}: {used_time:.1f}/{maxtime[j]} 小时\")\n",
    "    print(\"测试\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "math_model",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
